<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/5/16
 * Time: 17:21
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\admin\controller\mall;

use app\common\controller\AdminController;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\lib\Time;
use app\common\model\MallOrderGoods;
use app\common\model\WeixinExpressOrder;
use EasyAdmin\tool\CommonTool;
use jianyan\excel\Excel;
use think\App;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="快递统计")
 */
class FinanceOrder extends AdminController
{
    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new WeixinExpressOrder();

        $this->assign('getStatusList', $this->model->getStatusList());
    }

    /**
     * @NodeAnotation(title="统计列表")
     * @return array|mixed|\think\response\Json
     */
    public function index()
    {
        if($this->request->isAjax()){
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where,$map) = $this->buildTableParames(['title','count','money','create_time'],"o");
            if(empty($where)){
                $where[] = [
                    ['o.create_time','between',Time::month()]
                ];
            }
            if(!empty($map)){
                if (!empty($map['title'])) {
                    $where[] = [
                        ['s.title','like',"%{$map['title']}%"]
                    ];
                }
                if(!empty($map['create_time'])){
                    [$beginTime, $endTime] = explode(' - ', $map['create_time']);

                    $where[] = [
                        ['o.create_time','between',[
                            strtotime($beginTime),
                            strtotime($endTime)
                        ]]
                    ];
                }else{
                    $where[] = [
                        ['o.create_time','between',Time::month()]
                    ];
                }
            }

            $where[] = [
                ['pay_status','=',1]
            ];
            $lists = [];
            //商品总金额
            $lists = $this->model
                ->alias('o')
                ->field("o.*,sum(order_number) as count,sum(o.price) as money,s.title as title,sum(express_number) as number,sum(pay_number) as pay_number")
                ->join('mall_shop s',"o.shop_id = s.id")
                ->where($where)
                ->where('order_sn',0)
                ->group('o.shop_id')
                ->order('create_time desc,id desc')
                ->paginate($limit)->each(function ($item,$key){
                    //根据店铺id查询店铺的快递金额
                    return $item;
                })
                ->toArray();
            return Show::json_show(Status::lay_normal,'ok',$lists['data'],$lists['total']);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="导出")
     * @return bool
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function export()
    {
        $ids = $this->request->param('ids');

        list($page, $limit, $where) = $this->buildTableParames();
        if(!empty($ids)){
            $id = explode(',',$ids);

            if(!empty($id)){
                $order = $this->model->where('id','in',$id)->group('shop_id')->select()->toArray();

                if(!empty($order)){
                    $where[] = [
                        ['shop_id','in',array_column($order,'shop_id')]
                    ];
                }
            }
        }
        $where[] = [
            ['pay_status','=',1],
            ['order_sn','=',0]
        ];
        $tableName = $this->model->getName();
        $tableName = CommonTool::humpToLine(lcfirst($tableName));
        $prefix = config('database.connections.mysql.prefix');
        $dbList = Db::query("show full columns from {$prefix}{$tableName}");

        $header = [];
        foreach ($dbList as $vo) {
            $comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'];
            if (!in_array($vo['Field'], $this->noExportFields)) {
                $header[] = [$comment, $vo['Field']];
            }
        }
        $list = $this->model
            ->where($where)
            ->limit(100000)
            ->order('id', 'desc')
            ->select()
            ->toArray();
        $fileName = time();
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }
}